package com.resou.reader.mine.rechargerecord;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.PayRecordBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ${Garrett} on 2018/9/21.
 * Contact me krouky@outlook.com
 */
public class RechargeAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<PayRecordBean> mList = new ArrayList<>();
    private boolean hasMore = true;//是否有下一页

    private int normalType = 0;
    private int footType = 1;

    public RechargeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void updateList(List<PayRecordBean> beans, boolean hasMore) {
        boolean temp = (!this.hasMore && hasMore) || (this.hasMore && !hasMore);
        this.hasMore = hasMore;
        if (beans != null && beans.size() > 0) {
            mList.addAll(beans);
            notifyDataSetChanged();
        } else if (temp) {
            notifyItemChanged(getItemCount() - 1);
        }
    }

    public void clear() {
        mList.clear();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == normalType) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_recharge_layout, viewGroup, false));
        } else {
            return new FootHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_foot_layout, viewGroup, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i) == normalType) {
            ViewHolder ranViewHolder = (ViewHolder) viewHolder;
            PayRecordBean bean = mList.get(i);
            if (bean.getPayMethod().equals(PayRecordBean.PayMethod_AS)) {
                ranViewHolder.imgIcon.setVisibility(View.INVISIBLE);
                ranViewHolder.tvIcon.setVisibility(View.VISIBLE);
            } else {
                ranViewHolder.imgIcon.setVisibility(View.VISIBLE);
                ranViewHolder.tvIcon.setVisibility(View.INVISIBLE);
                ranViewHolder.imgIcon.setImageDrawable(
                        mContext.getResources().getDrawable(
                                PayRecordBean.PayMethod_WX.equals(bean.getPayMethod()) ?
                                        R.mipmap.ic_weixin :
                                        R.mipmap.ic_zhifubao
                        )
                );
            }

            ranViewHolder.recharge_num.setText(String.valueOf((int) bean.getRechargeAmount() + "元"));
            ranViewHolder.recharge_blance.setText((null == bean.getBookCoin() ? 0 : bean.getBookCoin()) + "热币");
            ranViewHolder.voucher.setText((null == bean.getGiftCoin() ? 0 : bean.getGiftCoin()) + "热券");
            SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy年MM月dd日");
            SimpleDateFormat hourFormat = new SimpleDateFormat("HH:mm");
            Date date = new Date(bean.getRechargeTime());
            ranViewHolder.year_date.setText(yearFormat.format(date));
            ranViewHolder.hour_date.setText(hourFormat.format(date));
        } else {
            FootHolder footHolder = (FootHolder) viewHolder;
            if (hasMore) {
                footHolder.moreText.setText("加载更多~");
            } else {
                footHolder.moreText.setText("没有更多了~");
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mList.size()) {
            return footType;
        } else {
            return normalType;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imgIcon;
        TextView tvIcon;
        TextView recharge_num;
        TextView recharge_blance;
        TextView year_date;
        TextView hour_date;
        TextView voucher;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgIcon = itemView.findViewById(R.id.imgIcon);
            tvIcon = itemView.findViewById(R.id.tvIcon);
            recharge_num = itemView.findViewById(R.id.recharge_num);
            recharge_blance = itemView.findViewById(R.id.recharge_blance);
            year_date = itemView.findViewById(R.id.year_date);
            hour_date = itemView.findViewById(R.id.hour_date);
            voucher = itemView.findViewById(R.id.voucher);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {

        TextView moreText;

        FootHolder(@NonNull View itemView) {
            super(itemView);
            moreText = itemView.findViewById(R.id.recycler_foot_text);
        }
    }
}
